﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SpringBeansEditor.Providers;
using System.Xml;

namespace SpringBeansEditor.Editors
{
    /// <summary>
    /// PropertyEditor allows users to update a XmlElement of type property.
    /// </summary>
    public partial class PropertyEditor : UserControl, IEditor
    {
        const string NAME_ATTR = "name";
        const string VALUE_ATTR = "value";
        const string REF_ATTR = "ref";
        const string OBJ_TAG_NAME = "object";
        const string OBJ_ID_ATTR = "id";

        XmlElement _xml_element_to_update;

        public PropertyEditor()
        {
            InitializeComponent();
        }

        public void OnAssemblyUpdated(TypesProvider reflection_provider)
        {
        }

        public void OnXmlUpdated(XmlDocument beans_xml_conf)
        {
            ObjectIDsLst.Items.Clear();
           XmlNodeList all_objects = beans_xml_conf.GetElementsByTagName(OBJ_TAG_NAME);
           foreach (XmlNode spring_object in all_objects)
           {
               ObjectIDsLst.Items.Add(spring_object.Attributes[OBJ_ID_ATTR].Value);
           }
        }

        public void ShowXMLElement(XmlElement object_element)
        {
            string parent_id = object_element.ParentNode.Attributes[OBJ_ID_ATTR].Value;
            TypesProvider reflection_provider = TypesProvider.GetInstance();
            NameCmb.ItemsSource = reflection_provider.GetTypeProperties(parent_id);

            NameCmb.SelectedItem = object_element.Attributes[NAME_ATTR].Value;
            NameCmb.Text = object_element.Attributes[NAME_ATTR].Value;
            if (object_element.HasAttribute(VALUE_ATTR))
            {
                ValueTxt.Text = object_element.Attributes[VALUE_ATTR].Value;
            }
            else
            {
                ValueTxt.Text = "";
            }
            if (object_element.HasAttribute(REF_ATTR))
            {
                RefTxt.Text = object_element.Attributes[REF_ATTR].Value;
            }
            else
            {
                RefTxt.Text = "";
            }
            _xml_element_to_update = object_element;
        }
        public void Show()
        {
            Visibility = System.Windows.Visibility.Visible;
        }
        public void Hide()
        {
            Visibility = System.Windows.Visibility.Collapsed;
        }

        private void SaveBtn_Click(object sender, RoutedEventArgs e)
        {
            _xml_element_to_update.Attributes[NAME_ATTR].Value = NameCmb.Text;
            //var val_attr = _xml_element_to_update.Attributes[VALUE_ATTR];
            if (ValueTxt.Text.Length > 0)
            {
                _xml_element_to_update.SetAttribute(VALUE_ATTR, ValueTxt.Text);
            }
            else
            {
                _xml_element_to_update.RemoveAttribute(VALUE_ATTR);
            }
            if (RefTxt.Text.Length > 0)
            {
                _xml_element_to_update.SetAttribute(REF_ATTR, RefTxt.Text);
            }
            else
            {
                _xml_element_to_update.RemoveAttribute(REF_ATTR);
            }
        }

        private void RefTxt_TextChanged(object sender, TextChangedEventArgs e)
        {
            ObjectIDsLst.Items.Filter = new Predicate<object>(NameFilter);
        }
        public bool NameFilter(object item)
        {
            string id = item as string;
            return (id.ToLower().Contains(RefTxt.Text.ToLower()));
        }

        /// <summary>
        /// Populates the Ref field when a user click on an ID listed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ObjectIDsLst_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string id = ((ListBox)sender).SelectedItem as string;
            if (id != null)
            {
                RefTxt.Text = id;
            }
        }
    }
}
